package com.saver.view.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.saver.R;
import com.saver.view.adapter.AjustesListAdapter.Group.Type;


public class AjustesListAdapter extends BaseExpandableListAdapter{

	/*
	 * view types
	 */
	private static final int REDES_SOCIALES_VIEW = 3;
	private static final int DATOS_USUARIO_VIEW = 2;
	private static final int MONEDAS_VIEW = 1;
	private static final int BANCOS_VIEW = 0;
	
	/*
	 * data
	 */
	private Context context = null;
	ArrayList<Group> groups = new ArrayList<Group>();
	
	public AjustesListAdapter(Context context){
		this.context = context;
	}
	
	@Override
	public int getChildType(int groupPosition, int childPosition) {
		int type = -1;
		if(groups.size() == 4 && groupPosition == 1){
			type = MONEDAS_VIEW;
		} else if(groups.size() == 4 && groupPosition == 0){
			type = BANCOS_VIEW;
		} else if(groups.size() == 4 && groupPosition == 2){
			type = DATOS_USUARIO_VIEW;
		}else {
			type = REDES_SOCIALES_VIEW;
		}
		
		return type;
	}

	@Override
	public int getChildTypeCount() {
		//Past and BANCO Travel Plans
		return 4;
	}
	
	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return groups.get(groupPosition).configuracionItems.get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		
		//get the type of the group this child belongs
		Type viewType = groups.get(groupPosition).type;
		View view = convertView;
		
		if(viewType == Type.BANCO) {
			if(view == null){
				view = LayoutInflater.from(context).inflate(R.layout.config_bancos_line_item, parent, false);
				
				BancosViewHolder holder = new BancosViewHolder();
				holder.image = (ImageView) view.findViewById(R.id.icon_bancos);
				holder.url = (TextView) view.findViewById(R.id.url);
				holder.nombre = (TextView) view.findViewById(R.id.nombre_banco);
				view.setTag(holder);
			}
			ConfiguracionItem currentItem = (ConfiguracionItem)getChild(groupPosition, childPosition);

			BancosViewHolder holder = (BancosViewHolder) view.getTag();
			holder.url.setVisibility(View.INVISIBLE);
			holder.url.setText(currentItem.getUrlBanco());
			holder.nombre.setText(currentItem.getNombreBanco());
			holder.image.setVisibility(View.INVISIBLE);
//			if(currentItem.getNombreBanco().equals("BBVA")){
////				holder.image.setImageResource(R.drawable.banco_bbva_peru);
//				holder.nombre.setTextColor(Color.BLUE);
//			}else if(currentItem.getNombreBanco().equals("Scotiabank")){
////				holder.image.setImageResource(R.drawable.banco_scotiabank_peru);
//				holder.nombre.setTextColor(Color.RED);
//			}else if(currentItem.getNombreBanco().equals("Interbank")){
////				holder.image.setImageResource(R.drawable.banco_interbank);
//				holder.nombre.setTextColor(Color.GREEN);
//			}
			holder.nombre.setTextColor(Color.BLACK);
		} else if(viewType == Type.MONEDA) {
			if(view == null){
				view = LayoutInflater.from(context).inflate(R.layout.config_monedas_line_item, parent, false);
				
				MonedasViewHolder holder = new MonedasViewHolder();
				holder.title = (TextView) view.findViewById(R.id.monedasTitle);
				view.setTag(holder);
			}
			
			MonedasViewHolder holder = (MonedasViewHolder) view.getTag();
			
			ConfiguracionItem currentItem = (ConfiguracionItem)getChild(groupPosition, childPosition);
			
			holder.title.setText(currentItem.getMoneda());

		}else if(viewType == Type.DATOS_USUARIO){
			if(view == null){
				view = LayoutInflater.from(context).inflate(R.layout.config_datos_usuario_line_item, parent, false);
				
				DatosUsuarioViewHolder holder = new DatosUsuarioViewHolder();
				holder.title = (TextView) view.findViewById(R.id.datosUsuarioTitle);
				view.setTag(holder);
			}
			
			DatosUsuarioViewHolder holder = (DatosUsuarioViewHolder) view.getTag();
			
			ConfiguracionItem currentItem = (ConfiguracionItem)getChild(groupPosition, childPosition);
			if(currentItem != null){
				holder.title.setText(currentItem.getIdUsuario());
			}
		}else if(viewType == Type.REDES_SOCIALES){
			if(view == null){
				view = LayoutInflater.from(context).inflate(R.layout.config_redes_sociales_line_item, parent, false);
				
				RedesSocialesViewHolder holder = new RedesSocialesViewHolder();
				holder.image = (ImageView) view.findViewById(R.id.icon_redes_sociales);
				holder.url = (TextView) view.findViewById(R.id.url_redes_sociales);
				holder.nombre = (TextView) view.findViewById(R.id.nombre_redes_sociales);
				view.setTag(holder);
			}
			ConfiguracionItem currentItem = (ConfiguracionItem)getChild(groupPosition, childPosition);

			RedesSocialesViewHolder holder = (RedesSocialesViewHolder) view.getTag();
			holder.url.setVisibility(View.INVISIBLE);
			holder.url.setText(currentItem.getUrlRedSocial());
			holder.nombre.setText(currentItem.getRedSocial());
			if(currentItem.getRedSocial().equals("Facebook")){
				holder.image.setImageResource(R.drawable.logo_facebook);
			}
		}
		return view;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return groups.get(groupPosition).configuracionItems.size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return groups.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return groups.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		
		View view = convertView;
		TextView text = null;
		ImageView image = null;
		
		if(view == null){
			view = LayoutInflater.from(context).inflate(R.layout.ajustes_list_group_view, parent, false);
		}
		
		text = (TextView) view.findViewById(R.id.groupHeader);
		image = (ImageView) view.findViewById(R.id.expandableIcon);
		
		StringBuilder title = new StringBuilder();
		if(groupPosition == 0){
			title.append(context.getString(R.string.bancos_list_header));
		} else if(groupPosition == 1) {
			title.append(context.getString(R.string.monedas_list_header));
		} else if (groupPosition == 2){
			title.append(context.getString(R.string.datos_usuario_list_header));
		}else{
			title.append(context.getString(R.string.redes_sociales_list_header));
		}
		
		text.setText(title.toString());

//		if(groupPosition != 0){
			int imageResourceId = isExpanded ? android.R.drawable.arrow_up_float : android.R.drawable.arrow_down_float;
			image.setImageResource(imageResourceId);
			
			image.setVisibility(View.VISIBLE);
//		} else {
//			image.setVisibility(View.INVISIBLE);
//		}
		return view;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}
	
	public void setupItems(ArrayList<ConfiguracionItem> monedas, 
						   ArrayList<ConfiguracionItem> bancos,
						   ArrayList<ConfiguracionItem> datosUsuario,
						   ArrayList<ConfiguracionItem> redesSociales){
		groups.clear();
		
		if(monedas != null && datosUsuario != null && redesSociales !=null){
			Group g1 = new Group();
			g1.type = Type.BANCO;
			g1.configuracionItems.clear();
			g1.configuracionItems = new ArrayList<ConfiguracionItem>(bancos);
			
			groups.add(g1);
		}
		if(bancos != null && datosUsuario != null && redesSociales != null){
			Group g2 = new Group();
			g2.type = Type.MONEDA;
			g2.configuracionItems.clear();
			g2.configuracionItems = new ArrayList<ConfiguracionItem>(monedas);
			
			groups.add(g2);
		}
		if(bancos != null && monedas != null && redesSociales != null){
			Group g3 = new Group();
			g3.type = Type.DATOS_USUARIO;
			g3.configuracionItems.clear();
			g3.configuracionItems = new ArrayList<ConfiguracionItem>(datosUsuario);
			
			groups.add(g3);
		}
		if(bancos != null && monedas != null && datosUsuario != null){
			Group g4 = new Group();
			g4.type = Type.REDES_SOCIALES;
			g4.configuracionItems.clear();
			g4.configuracionItems = new ArrayList<ConfiguracionItem>(redesSociales);
			
			groups.add(g4);
		}
		
		notifyDataSetChanged();
	}
	
	class MonedasViewHolder {
		TextView title;
	}
	
	class DatosUsuarioViewHolder {
		TextView title;
	}
	
	public class BancosViewHolder {
		public ImageView image;
		public TextView nombre;
		public TextView url;
	}
	public class RedesSocialesViewHolder {
		public ImageView image;
		public TextView nombre;
		public TextView url;
	}
	
	public static class Group {
		public enum Type {
			MONEDA, BANCO, DATOS_USUARIO, REDES_SOCIALES;
		};
		
		public Type type;
		ArrayList<ConfiguracionItem> configuracionItems = new ArrayList<ConfiguracionItem>();
	}
}